package es

import (
	"context"
	"github.com/olivere/elastic/v7"
	"log/lg"
)

var Client *elastic.Client

func EsInit(host string) {
	var err error
	//初始化es客户端
	Client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
	if err != nil {
		lg.Log.Error("链接es出错请检查", err)
		return
	}
	//检查当前es是否正常
	info, code, err := Client.Ping(host).Do(context.Background())
	if err != nil {
		lg.Log.Error("链接es出错请检查", err)
		return
	}
	lg.Log.Info("es访问正常:", code, ",当前链接的es版本是:", info.Version.Number)

}

// EsAdd 插入数据
func EsAdd(json string) {

	data, err := Client.Index().
		Index("jjg").
		BodyJson(json).
		Do(context.Background())
	if err != nil {
		lg.Log.Error("es数据插入错误", err)
		return
	}
	lg.Log.Info("es数据插入成功", data)

}
